import SocreHint from "./SocreHint";

/**
*
* @ author: hxwy
* @ email: 928936715@qq.com
* @ date: 2020-04-13 16:28
*/
export default class PlayerCtrl extends Laya.Script3D {

    ctrl:Laya.CharacterController;
    col:Laya.SphereColliderShape;
    physicsSimulation:Laya.PhysicsSimulation;
    ray:Laya.Ray;
    hitResult:Laya.HitResult;
    transform:Laya.Transform3D;
    ThrougCound : number = 0;
    socreHint : SocreHint;
    constructor() {
        super();
    }
    onAwake()
    {
        this.socreHint = SocreHint.GetIniastanc();
        this.socreHint.init();

        this.ctrl = this.owner.addComponent(Laya.CharacterController);
        this.transform = (this.owner as Laya.Sprite3D).transform;
        this.col = new Laya.SphereColliderShape(0.5);
        this.ctrl.colliderShape = this.col;
        console.log();
        this.physicsSimulation = ((this.owner as Laya.Sprite3D).parent as Laya.Scene3D).physicsSimulation;
        this.ray = new Laya.Ray(new Laya.Vector3(),new Laya.Vector3(0,-1,0));
        this.hitResult = new Laya.HitResult();
        
        Laya.timer.frameLoop(1,this,this.RayCast);

        Laya.stage.on("ContinueGame",this,function()
        {
            this.transform.position = new Laya.Vector3(0,0,-0.75);
            this.transform.localScaleY = 0.3;
        });
    }
    RayCast()
    {
        this.ray.origin = this.transform.position;

        if(this.ThrougCound >= 3 && this.physicsSimulation.rayCast(this.ray,this.hitResult,0.2))
        {
            let col = this.hitResult.collider as Laya.PhysicsCollider;
            let num = col.owner.parent.numChildren;
            for (let i = 0; i < num; i++) {
                let child = col.owner.parent.getChildAt(i) as Laya.MeshSprite3D; 
                child.name = "Bar";
                (child.meshRenderer.material as Laya.PBRStandardMaterial).albedoColor = new Laya.Vector4(0,0,1,1);             
            }
            //Laya.stage.event("CreateTxt_Hint",this.ThrougCound);
        }


        if(this.physicsSimulation.rayCast(this.ray,this.hitResult,0.15))
        {
            let col = this.hitResult.collider as Laya.PhysicsCollider;
            if(col.owner.name == "obstacles")
            {
                this.transform.localScaleY = 0.15;
                Laya.stage.event("GameOver");
                return;
            }
            if(col.isTrigger)
            {
                this.ThrougCound++;
                (col.owner as Laya.Node).parent.removeSelf();
                Laya.Pool.recover("Platform",(col.owner as Laya.Node).parent);

                let data = ((col.owner as Laya.Node).parent as Laya.Sprite3D).transform.localPositionY;
                Laya.stage.event("MoveCamera", data);

                Laya.stage.event("CreatePlatform");

                Laya.stage.event("ColumnMove");
                
                this.socreHint.ShowSocre(1);

                Laya.SoundManager.playSound("res/sounds/DestSound.mp3",1);

            }
            else
            {
                if(this.ThrougCound >= 3)this.socreHint.ShowSocre(this.ThrougCound);
               
                this.ThrougCound = 0;
                Laya.stage.event("CreatePrticle",this.hitResult.point);
                this.ctrl.jump();
                this.ctrl.fallSpeed = 10;
                this.ctrl.jumpSpeed = 7; 
                Laya.SoundManager.playSound("res/sounds/jumpSound.mp3",1);
            }
        }
    }

}
